import { defineStore } from 'pinia'
import { computed, ref } from 'vue'

/**
 * 参数1：仓库的名字
 * 参数2：编写仓库逻辑的函数
 * 参数3：设置持久化，后面再说
 *
 * 注意点：定义仓库的函数名字以use开头
 */
export const useCounterStore = defineStore('counter', () => {
  // state
  const count = ref(100)
  // getter
  const doubleCount = computed(() => count.value * 2)
  // mutations
  const changeCount = (val: number) => {
    count.value = val
  }
  // actions
  const asyncChangeCount = (val: number) => {
    setTimeout(() => {
      count.value = val
    }, 2000)
  }

  // 别忘记返回了
  return {
    count,
    doubleCount,
    changeCount,
    asyncChangeCount
  }
})
